<?php

namespace app\api\controller;

use think\facade\Cache;
use think\facade\View;
use think\facade\Db;

class Callback
{


    public function index()
    {

//公众号
        $appid = 'wxcf34f8be3e7a4461';
        $secret = '85a7fee4834d3b9ef34cf9315f40919f';
        $id = $_REQUEST['id'];


        $code = $_REQUEST['code'];

        $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code';
        $res = file_get_contents($get_token_url);


        $json_obj = json_decode($res, true);

        if (isset($json_obj['openid'])) {
            $openid['openid'] = $json_obj['openid'];
            $openid['update_time'] = time();
            Db::name('customer_user')->where('id', $id)->update($openid);
            echo "<script>alert('绑定成功')</script>";
            die();
        }
        echo "<script>alert('绑定失败')</script>";


    }

    public function getcode()
    {

        $id = $_REQUEST['id'];
        $appid = "wxcf34f8be3e7a4461";
        $redirect_uri = "https://apiqixuke.qishenghui.net/callback/index?id=" . $id;
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        Header("Location: $url");
        exit();
    }


}